class BlogsController < ApplicationController
  # GET /blogs
  # GET /blogs.xml
  before_filter :init
  
  def index
    @blogs = Blog.paginate_by_accounts_id params[:member_id], :page => params[:page], :order => 'updated_at DESC'
    
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @blogs }
    end
  end

  # GET /blogs/1
  # GET /blogs/1.xml
  def show
    @blog = Blog.find(params[:id])
    @blog.increment(:view)

    @comments = @blog.comments.paginate_by_commentable_id params[:id], :page => params[:page]
    
    respond_to do |format|
      if @blog.save
        format.html # show.html.erb
        format.xml  { render :xml => @blog }
      end
    end
  end

  # GET /blogs/new
  # GET /blogs/new.xml
  def new
    @blog = Blog.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @blog }
    end
  end

  # GET /blogs/1/edit
  def edit
    @blog = Blog.find(params[:id])
  end

  # POST /blogs
  # POST /blogs.xml
  def create
    @blog = current_accounts.blogs.build(params[:blog])

    respond_to do |format|
      if @blog.save
        flash[:notice] = 'Blog was successfully created.'
        format.html { redirect_to(member_blog_path(@member,@blog)) }
        format.xml  { render :xml => @blog, :status => :created, :location => @blog }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @blog.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /blogs/1
  # PUT /blogs/1.xml
  def update
    @blog = Blog.find(params[:id])

    respond_to do |format|
      if @blog.update_attributes(params[:blog])
        flash[:notice] = 'Blog was successfully updated.'
        format.html { redirect_to(member_blog_path(@member,@blog)) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @blog.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /blogs/1
  # DELETE /blogs/1.xml
  def destroy
    @blog = Blog.find(params[:id])
    @blog.destroy

    respond_to do |format|
      format.html { redirect_to(blogs_url) }
      format.xml  { head :ok }
    end
  end

  def comment
    commentable = Blog.find params[:id]
    @comment = commentable.comments.create(:comment => params[:comment][:comment],:accounts_id => current_accounts.id)

    render :partial => "shared/comment_review"
  end

  protected

  def init
    @styleName = "my_blog"
    @member = Member.find params[:member_id]
  end
  
end
